package cn._2dland.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.json.JSONObject;
import org.w3c.dom.Document;

public class HttpUtils {
	/** 获取远程数据 */
	public static byte[] getHTTPBytes(String url) throws Exception {
		URL u = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)u.openConnection();
		conn.setDoInput(true);
		conn.connect();

		int respCode = conn.getResponseCode();
		if(respCode != HttpURLConnection.HTTP_OK)
			throw new Exception("connect failed: " + respCode);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		InputStream is = conn.getInputStream();
		for(int b; (b=is.read())>=0; )
			baos.write(b);

		baos.flush();
		byte[] data = baos.toByteArray();
		baos.close();
		is.close();
		conn.disconnect();

		return data;
	}

	/** 获取远程XML对象 */
	public static Document getHTTPXMLDocument(String url) throws Exception {
		URL u = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)u.openConnection();
		conn.setDoInput(true);
		conn.setRequestProperty("Accept-Encoding", "gzip");
		conn.connect();

		int respCode = conn.getResponseCode();
		if(respCode != HttpURLConnection.HTTP_OK)
			throw new Exception("connect failed: " + respCode);

		// 解压数据流
		InputStream xmlIS = conn.getInputStream();
		String contEnc = conn.getHeaderField("Content-Encoding");
		if(contEnc != null && contEnc.indexOf("gzip")>=0){
			xmlIS = new GZIPInputStream(conn.getInputStream());
		}

		DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document doc = db.parse(xmlIS);

		xmlIS.close();
		conn.disconnect();

		return doc;
	}

	/** 获取远程文本内容 */
	public static String getHTTPContent(String url) throws Exception {
		// 建立HTTP连接
		URL u = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)u.openConnection();
		conn.setDoInput(true);
		conn.connect();

		// 发送请求
		int respCode = conn.getResponseCode();
		if(respCode != HttpURLConnection.HTTP_OK)
			throw new Exception("connect failed: " + respCode);

		// 获取结果流
		InputStream is = conn.getInputStream();
		// 判断是否需要进行gzip解码
		String contEnc = conn.getHeaderField("Content-Encoding");
		if(contEnc != null && contEnc.indexOf("gzip")>=0){
			is = new GZIPInputStream(conn.getInputStream());
		}

		// 读取结果内容
		StringBuffer strBuff = new StringBuffer();
		InputStreamReader isr = new InputStreamReader(is);
		char[] cbuf = new char[32];
		int len = 0;
		do {
			strBuff.append(cbuf, 0, len);
			len = isr.read(cbuf);
		}while(len>=0);
		// 释放资源
		is.close();
		isr.close();
		conn.disconnect();

		return strBuff.toString();
	}

	/** 获取远程JSON对象 */
	public static JSONObject getHTTPJSONObject(String url) throws Exception {
		URL u = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)u.openConnection();
		conn.setDoInput(true);
		conn.connect();

		int respCode = conn.getResponseCode();
		if(respCode != HttpURLConnection.HTTP_OK)
			throw new Exception("connect failed: " + respCode);

		StringBuffer strBuff = new StringBuffer();
		InputStreamReader isr = new InputStreamReader(conn.getInputStream());
		char[] cbuf = new char[32];
		int len = 0;
		do{
			strBuff.append(cbuf, 0, len);
			len = isr.read(cbuf);
		}while(len>=0);
		isr.close();
		conn.disconnect();

		JSONObject jsonObj = new JSONObject(strBuff.toString());
		return jsonObj;
	}
}
